﻿Imports System.Globalization
Imports System.Threading

<ValidationProperty("Text")> _
Partial Class WebForm_CommonControl_CalendarTextBox
    Inherits MyCalendarTextBoxBase

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.LoadData()
    End Sub

    Protected Sub Page_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DataBinding
        Me.LoadData()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            Me.LoadData()
        End If

    End Sub

    Private Sub LoadData()
        If Me.ddlYear.Items.Count = 0 Then
            Me.BindDDLYear()
            Me.BindDDLMonth()
            Me.BindDDLDay()
        End If
    End Sub

    Private Sub BindDDLYear()
        'Dim d As New Date(Me.MaxYear, 1, 1)
        Dim d As Date = Date.Now.AddYears(Me.MaxYearSteps)

        'For i As Integer = -1 To Me.YearSteps
        For i As Integer = d.Year To Me.MinYear Step -1
            Dim li As New ListItem

            li.Text = d.ToString("yyyy")
            li.Value = d.Year

            Me.ddlYear.Items.Add(li)

            d = d.AddYears(-1)
        Next

        Me.ddlYear.SelectedValue = Me.InitValue.Year
    End Sub

    Private Sub BindDDLMonth()
        For i As Integer = 1 To 12
            Dim li As New ListItem

            li.Text = i.ToString("00")
            li.Value = i

            Me.ddlMonth.Items.Add(li)
        Next

        Me.ddlMonth.SelectedValue = Me.InitValue.Month

    End Sub

    Private Sub BindDDLDay()
        Dim c As Calendar = Thread.CurrentThread.CurrentCulture.DateTimeFormat.Calendar

        Me.ddlDay.Items.Clear()

        For i As Integer = 1 To c.GetDaysInMonth(Me.ddlYear.SelectedValue, Me.ddlMonth.SelectedValue)
            Dim li As New ListItem

            li.Text = i.ToString("00")
            li.Value = i

            Me.ddlDay.Items.Add(li)
        Next

        If Me.ddlYear.SelectedValue = Me.InitValue.Year And Me.ddlMonth.SelectedValue = Me.InitValue.Month Then
            Me.ddlDay.SelectedValue = Me.InitValue.Day
        End If

    End Sub

    Protected Sub ddlMonth_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlMonth.SelectedIndexChanged
        Me.BindDDLDay()
    End Sub

    Protected Sub ddlYear_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlYear.SelectedIndexChanged
        Me.BindDDLDay()
    End Sub

    Public Property Text() As String
        Get
            Return Me.Value.ToString("yyyy/MM/dd")
        End Get
        Set(ByVal value As String)
            Try
                Me.Value = DateTime.Parse(value)
            Catch ex As Exception

            End Try

        End Set
    End Property

    Public Overrides Property Value() As Date
        Get
            If Me.RequiresDataBinding = True Then
                Me.LoadData()
            End If

            Return (New Date(Me.ddlYear.SelectedValue, Me.ddlMonth.SelectedValue, Me.ddlDay.SelectedValue))
        End Get
        Set(ByVal value As Date)
            Me.LoadData()

            Me.ddlYear.SelectedValue = value.Year
            Me.ddlMonth.SelectedValue = value.Month

            Me.ddlMonth_SelectedIndexChanged(Nothing, Nothing)
            Me.ddlDay.SelectedValue = value.Day
        End Set
    End Property

    Public Property InitValue() As Date
        Get
            If ViewState("InitValue") Is Nothing Then
                ViewState("InitValue") = DateTime.Now
            End If

            Return ViewState("InitValue")
        End Get
        Set(ByVal value As Date)
            ViewState("InitValue") = value
        End Set
    End Property

    Public Property YearSteps() As Integer
        Get
            If ViewState("YearSteps") Is Nothing Then
                Return 10
            End If
            Return ViewState("YearSteps")
        End Get
        Set(ByVal value As Integer)
            ViewState("YearSteps") = value
        End Set
    End Property

    Public Property MaxYearSteps() As Integer
        Get
            If ViewState("MaxYearSteps") Is Nothing Then
                Return 1
            End If
            Return ViewState("MaxYearSteps")
        End Get
        Set(ByVal value As Integer)
            ViewState("MaxYearSteps") = value
        End Set
    End Property

    Public Property MinYear() As Integer
        Get
            If ViewState("MinYear") Is Nothing Then
                Return 2000
            End If
            Return ViewState("MinYear")
        End Get
        Set(ByVal value As Integer)
            ViewState("MinYear") = value
        End Set
    End Property

    Public ReadOnly Property RequiresDataBinding() As Boolean
        Get
            If Me.ddlYear.Items.Count = 0 Then
                Return True
            Else
                Return False
            End If
        End Get
    End Property

End Class
